package com.kaizige.realtor.Controller;

import com.kaizige.realtor.Dao.houseMsgDao;
import com.kaizige.realtor.Dao.messageDao;
import com.kaizige.realtor.Pojo.message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class updateController {
	@Autowired
	houseMsgDao hmd;
	@Autowired
	messageDao md;
	@GetMapping("/ownUpdate")
	public String toOwnUpdate(HttpSession session, Model model){
		List<Map<String, Object>> mapList = hmd.queryHmByOwner((String)session.getAttribute("LoginUser"));
		model.addAttribute("houses",mapList);
		return "/crud/ownUpdate";
	}
	@PostMapping("/ownUpdate")
	public String ownUpdate(@RequestParam("id")int id,HttpSession session){
		String houseOwner = (String)session.getAttribute("LoginUser");
		message msg = new message(houseOwner, (String) session.getAttribute("identity"), "admin1", "manager",
				"房主" + houseOwner + "请求变更房屋" + id + "的租赁状态");
		md.ownToManUp(msg);
		return "redirect:/ownMessage";
	}
	
	@GetMapping("/manUpHouSta")
	public String toManUpHouSta(Model model){
		List<Map<String, Object>> maps = hmd.queryAllMsg();
		model.addAttribute("houses",maps);
		return "/crud/manUpHouStatus";
	}
	
	@PostMapping("/manUpHouSta")
	public String manUpHouSta(@RequestParam("toSta") int toSta,@RequestParam("id")int id){
		hmd.updateHouStaByID(toSta,id);
		return "redirect:/manIndex";
	}
}